﻿@model IEnumerable<QuanLyGiaoVuUI.Models.TaiLieu.DanhSachTaiLieuViewModel>

@*@Html.Grid(Model).Columns(column =>
{
    column.For(tailieu => tailieu.MaTaiLieu).Visible(false);
    column.For(tailieu => tailieu.TenTaiLieu).Named("Tên tài liệu").HeaderAttributes(new Dictionary<string, object> { { "scope", "col" } });
    column.For(tailieu => tailieu.LoaiTaiLieu.TenLoaiTaiLieu).Named("Loại tài liệu").HeaderAttributes(new Dictionary<string, object> { { "scope", "col" } });
    column.For(tailieu => tailieu.GhiChu).Named("Mô tả").HeaderAttributes(new Dictionary<string, object> { { "scope", "col" } });
    column.For(tailieu => Html.ActionLink(tailieu.TenTapTin + tailieu.PhanMoRong, "TaiFile", "TaiLieu", new { fileid = tailieu.MaTapTin }, null)).Named("Link").DoNotEncode().HeaderAttributes(new Dictionary<string, object> { { "scope", "col" } });
    column.For(tailieu => "<a href='#' onclick=\"SuaTaiLieu('" + tailieu.MaTaiLieu + "')\">Sửa</a><a href='#' onclick=\"XoaTaiLieu('" + tailieu.MaTaiLieu + "','" + tailieu.TenTaiLieu + "')\">Xóa</a>").Encode(false).HeaderAttributes(new Dictionary<string, object> { { "scope", "col" } });
}).Attributes(new Dictionary<string, object> { { "id", "gradient-style" }, {"class","data display datatable"}})*@

<table class="data display datatable">
    <tr>
        <th class="sorting"></th>
        <th class="sorting">Tên tài liệu
        </th>
        <th class="sorting">Loại tài liệu
        </th>
        <th class="sorting">Mô tả
        </th>
        <th class="sorting">Tải file
        </th>
        <th class="sorting">Tùy chọn
        </th>
    </tr>
    @{int count = 0;}
    @foreach (var item in Model)
    {
        <tr class="gradeA @(++count % 2 == 0 ? "even" : "odd")">
            <td>
                <input type="checkbox" value="@item.MaTaiLieu" style="display:none" />
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TenTaiLieu)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LoaiTaiLieu.TenLoaiTaiLieu)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.GhiChu)
            </td>
            <td>
                <a href='@Url.Action("TaiFile", "TaiLieu", new { 
                        fileId = item.MaTapTin
                    })'>
                    <img src="~/Content/themes/bluewhale/img/button_download.png" />
                </a>
            </td>
            <td>
                <a href='#' onclick="@String.Format("SuaTaiLieu('{0}')",item.MaTaiLieu)">Sửa</a>| 
                <a href='#' onclick="@String.Format("XoaTaiLieu('{0}','{1}')",item.MaTaiLieu,item.TenTaiLieu)">Xóa</a>
            </td>
        </tr>
    }

</table>
<script>
    function XoaTaiLieu(maTaiLieu, tenTaiLieu) {
        if (confirm("Bạn có muốn xóa " + tenTaiLieu + "?")) {
            $.ajax({
                url: "/TaiLieu/XoaTaiLieu",
                type: "POST",
                data: {
                    maTaiLieu: maTaiLieu
                },
                success: function (data) {
                    if (data.success) {
                        ToastManager.showSuccessToast("Xóa thành công.");
                        $("#layDanhSachTaiLieu").click();
                    }
                    else
                        ToastManager.showStickyWarningToast(data.message);
                }
            });
        }
    }

    function SuaTaiLieu(maTaiLieu) {
        TaiLieuManager.CapNhatTaiLieu(maTaiLieu);
    }
</script>
